/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package messagediven;

import common.EmailCreator;
import entity.Persona;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 *
 * @author Valerio
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class EmailManager implements EmailManagerLocal {
    @Resource(mappedName = "jms/EmailSender")
    private Queue emailSender;
    @Resource(mappedName = "jms/EmailSenderFactory")
    private ConnectionFactory emailSenderFactory;
    
    @Override
    public void inviaEmailReimpostaPassword(Persona persona) {
        String email;
        try {
            email= EmailCreator.creaEmailReimpostaPassword(persona);
            sendJMSMessageToEmailSender(email);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(EmailManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(EmailManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JMSException ex) {
            Logger.getLogger(EmailManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private Message createJMSMessageForjmsEmailSender(Session session, Object messageData) throws JMSException {
        // TODO create and populate message to send
        TextMessage tm=session.createTextMessage();
        tm.setText(messageData.toString());
        return tm;
    }

    private void sendJMSMessageToEmailSender(Object messageData) throws JMSException {
        Connection connection = null;
        Session session = null;
        try {
            connection = emailSenderFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(emailSender);
            messageProducer.send(createJMSMessageForjmsEmailSender(session, messageData));
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}
